package org.yenn.pattern.aliyun.behavioral.chain;

import org.junit.Test;
import org.yenn.pattern.aliyun.behavioral.chain.impl.ConsoleLogger;
import org.yenn.pattern.aliyun.behavioral.chain.impl.ErrorLogger;
import org.yenn.pattern.aliyun.behavioral.chain.impl.FileLogger;

import static org.junit.Assert.*;

/**
 * 步骤 3
 * 创建不同类型的记录器。赋予它们不同的错误级别，并在每个记录器中设置下一个记录器。每个记录器中的下一个记录器代表的是链的一部分。
 */
public class ChainPatternDemo {
    private static AbstractLogger getChainOfLoggers() {

        AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
        AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
        AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);

        errorLogger.setNextLogger(fileLogger);
        fileLogger.setNextLogger(consoleLogger);

        return errorLogger;
    }

    /**
     * 步骤 4
     * 验证输出。
     * Standard Console::Logger: This is an information.
     * File::Logger: This is an debug level information.
     * Standard Console::Logger: This is an debug level information.
     * Error  Console::Logger: This is an error information.
     * File::Logger: This is an error information.
     * Standard Console::Logger: This is an error information.
     */
    @Test
    public void logMessage() {
        AbstractLogger loggerChain = getChainOfLoggers();

        loggerChain.logMessage(AbstractLogger.INFO, "This is an information.");

        loggerChain.logMessage(AbstractLogger.DEBUG, "This is an debug level information.");

        loggerChain.logMessage(AbstractLogger.ERROR, "This is an error information.");
    }
}